import com.sun.jna.Callback;
import com.sun.jna.Library;
import com.sun.jna.Native;
import com.sun.jna.Pointer;
import com.sun.jna.ptr.IntByReference;
import com.sun.jna.ptr.ByteByReference;
import com.sun.jna.ptr.PointerByReference;

import java.io.UnsupportedEncodingException;
import java.lang.management.ManagementFactory;
import java.lang.management.RuntimeMXBean;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;

import static java.lang.Thread.sleep;

public class ProxyDriverJNA {
    public interface Clibrary extends Library {
        Clibrary INSTANTCE  = (Clibrary) Native.loadLibrary("D:/work/proxy-driver/cmake-build-debug-mingw/libProxyDriver", Clibrary.class);

        public interface PDFunc extends Callback{
            int message_callback(String data, int protocol);
        }

        void install_message_callback(PDFunc func);

        void test(String tt);

        int init_driver(String proxy_name);
        int parse_data(String data);
        int get_parse_result(PointerByReference data, IntByReference protocol);
        int pack_data(String  data, int protocol);
        int get_pack_result(PointerByReference data);

        int create_device(int type, String name, String parm);

        int select_device(String name);

        int release_driver();
    }



    public static void main(String[] args) throws InterruptedException, UnsupportedEncodingException {


        String icp = "{\"num\":0,\"deviceData\":\"$BDICP,123456,244.244.244.244,1,3,0,E,1,Y,1,0,0,1,1,5,2,1,0,4,2,10,1,20,8,0,998877*4D\\r\\n\",\"userData\":\"\",\"direction\":2,\"uuid\":\"uuid\",\"guid\":\"guid\"}";

        String tcq = "{\"num\":0,\"deviceData\":\"\",\"userData\":\"{\\\"uuid\\\":\\\"\\\",\\\"guid\\\":\\\"\\\",\\\"enumType\\\":8,\\\"recverID\\\":\\\"100002\\\",\\\"freq\\\":0,\\\"confirm\\\":1,\\\"codec\\\":6,\\\"data\\\":\\\"郭增权\\\",\\\"frameFreq\\\":0}\",\"direction\":1,\"uuid\":\"uuid\",\"guid\":\"guid\"}";

        byte[] bytes = "你好".getBytes("GBK");

        Clibrary.INSTANTCE.init_driver("BD3");

        Clibrary.INSTANTCE.install_message_callback(new Clibrary.PDFunc() {
            @Override
            public int message_callback(String data, int protocol) {
                System.out.println(data);
                System.out.println(protocol);
                return 0;
            }
        });
        RuntimeMXBean runtimeMXBean = ManagementFactory.getRuntimeMXBean();
        System.out.println(runtimeMXBean.getName());

        int ci = Clibrary.INSTANTCE.create_device(1,"udp1","224.224.224.224:9001");
        int si = Clibrary.INSTANTCE.select_device("udp1");
        while(true){
            Clibrary.INSTANTCE.pack_data(tcq,8);
            sleep(100);
        }
//        while (true){
//
//
//
//            //Clibrary.INSTANTCE.parse_data(icp);
//            PointerByReference data = new PointerByReference();
//            IntByReference protocol = new IntByReference();
//            int b = Clibrary.INSTANTCE.get_parse_result(data, protocol);
//
//
//            if(b != 0) {
//
//                String strPack = new String();
//                strPack = data.getValue().getString(0);
//                int intProtocol = protocol.getValue();
//            }
//            sleep(100);
//            if(b != 0){
//                if(data.getValue() != null){
//                    //System.out.println(data.getValue().getString(0));
//                    String strPack = new String();
//                    strPack = data.getValue().getString(0);
//                    int intProtocol = protocol.getValue();
//                    Clibrary.INSTANTCE.pack_data(strPack,intProtocol);
//                }
//            }
//
//            PointerByReference data1 = new PointerByReference();
//            b = Clibrary.INSTANTCE.get_pack_result(data1);
//            if(b != 0){
//                if(data1.getValue() != null){
//                    System.out.println(data1.getValue().getString(0));
//                }
//            }
        }
//    }

}
